<?php

Namespace Cyfy\Modules\Forms;
use \Cyfy\Message;
use \Cyfy\Modules\Forms\Module;

/**
 * Select element class.
 *
 * @author Dandelion <dandelion8888@gmail.com>
 * @version 0.1
 */
class Select extends Input
{
    /**
     * Data of the select in an associative array key => value
     * @var array
     */
    public $data = array();
    
    /**
     * Style of the rows, in the SAME sequence as $data.
     * @var array
     */
    public $dataStyle = array();
    
    /**
     * Multiple choice select
     * @var bool
     */
    public $multiple = false;
    
    /** 
     * On change JavaScript code to be called.
     * @var string
     */
    public $onChange = '';
    
    /**
     * Creates a new select element.
     *
     * @param string Select name
     * @param string Title
     * @param string Data in an associative array
     * @param string Default value - will be checked
     * @param bool Is required
     */    
    function __construct($name, $title, $data=array(), $value="", $required=false)
    {
        $this -> name = $name;
        $this -> title = $title;
        $this -> data = $data;
        $this -> required = $required;
        $this -> value = $value;
    }
    
    /**
     * Returns a string containing the radio.
     * @return string HTML content
     */
    function __toString()
    {
        $required = ($this->required) ? " form-validate" : "";
        $star = ($this->required) ? Module :: REQUIRED_CHAR : "";
        
        // multiple options?
        if($this -> multiple)
        {
            $opt = "name=\"{$this->name}[]\" multiple=\"multiple\" size=\"".count($this->data)."\"";
        }
        else
        {
            $opt = "name=\"{$this->name}\" size=\"1\"";
        }
        
        // onChange?
        if($this -> onChange)
        {
            $opt .= " onchange=\"{$this -> onChange}\"";
        }
        
         // select element
        $select = "    <select class=\"form-select-{$this->name}{$required}\" {$opt}>\n";
        $i = 0;
        foreach($this -> data as $name => $value){
            
            $s = "";
            // multiple selected?
            if(is_array($this -> value)){
                $s .= (in_array($name, $this -> value)) ? " selected = \"selected\"" : "";
            }else{
                $s .= ($this -> value == $name) ? " selected = \"selected\"" : "";
            }
            
            // style
            if($this -> dataStyle[$i]){
                $s .= " style = \"{$this -> dataStyle[$i]}\"";
            }
            
            $select .= "        <option value=\"{$name}\"{$s}>$value</option>\n";
            $i++;
        }
        $select .= "    </select>\n";
        
        // finishing
        $output = "<div class=\"form-item form-item-{$this->name}\"> \n";
        $output .= "    <label class=\"form-label-{$this->name}{$required}\">".$this -> title  . ": ". $star."</label> \n";
        $output .= "	{$select}";
        $output .= "</div> \n\n";
        return $output;
    }
    
    /** 
     * Validates the data.
     * @return bool
     */
    function validate()
    {
        // required
        if($this->required)
        {
            if(!$this -> value)
            {
                Message :: set(\Cyfy\Modules\Forms\Module :: t(121, array($this -> title)), Message::ERROR);
                return false;
            }
        }
        return true;
    }
}